package cn.jslancer.spider;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author jslancer
 * @date 2017/11/5
 */
public class HttpClientDownloader implements Downloader {

    @Override
    public String download(String url) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create()
                .build();
        //创建HttpGet请求
        HttpGet httpGet = new HttpGet(url);
        //发送请求，获取响应
        HttpResponse httpResponse = httpClient.execute(httpGet);
        //解析响应
        return EntityUtils.toString(httpResponse.getEntity(), "utf-8");
    }
}
